package com.molichuxing.gateway.bff.risk.services.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.risk.convertor.Convert;
import com.molichuxing.gateway.bff.risk.entity.request.create.BlacklistCreateVo;
import com.molichuxing.gateway.bff.risk.entity.request.modify.BlacklistModifyVo;
import com.molichuxing.gateway.bff.risk.entity.response.BlacklistVo;
import com.molichuxing.gateway.bff.risk.entity.response.CreditRecognitionVo;
import com.molichuxing.gateway.bff.risk.services.BlacklistBffService;
import com.molichuxing.services.business.dto.response.UserBizDto;
import com.molichuxing.services.business.service.UserBizService;
import com.molichuxing.services.infrastructure.dto.request.create.BlacklistCreateDto;
import com.molichuxing.services.infrastructure.dto.response.BlacklistDto;
import com.molichuxing.services.infrastructure.dto.response.CreditRecognitionDto;
import com.molichuxing.services.infrastructure.dto.response.UserDto;
import com.molichuxing.services.infrastructure.service.BlacklistService;
import com.molichuxing.services.infrastructure.service.CreditRecognitionService;
import com.molichuxing.services.infrastructure.service.UserService;
import com.molichuxing.services.property.BlacklistTypeEnum;

/**
 * 
 * @author liyg
 * 2019-08-31
 * 黑名单bff服务实现类
 * 
 */
@Service("blacklistBffService")
public class BlacklistBffServiceImpl implements BlacklistBffService{

	@Resource
	private BlacklistService blacklistService;
	
	@Resource
	private UserBizService userBizService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private CreditRecognitionService creditRecognitionService;
	
	/**
	 * 获取黑名单列表
	 * 
	 * @param map
	 *            参数
	 *key:userName
	 *            String 用户名
	 *key:name
	 *            String 用户姓名
	 *key:phone
	 *            String 用户手机号
	 *key:idNumber
	 *            String 身份证号
	 *key:startTime
	 *            LocalDateTime 拉黑开始时间
	 *key:endTime
	 *            LocalDateTime 拉黑结束时间
	 *key:type
	 *            Integer 拉黑方式
	 *key:pageNum
	 *            Integer 当前页
	 *key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<BlacklistVo> getBlacklistList(Map<String, Object> map) {
		
		String userName = ObjectCastUtil.castString(map.get("userName"));
		String phone = ObjectCastUtil.castString(map.get("phone"));
		
		if(StringUtils.isNoneBlank(userName)) {
			UserDto user = userService.getUserByName(userName);
			if(null == user || null == user.getId()) {
			  return new Paged<>();
			}
			
			//要根据用户手机号查询用户是否被拉入黑名单
			if(StringUtils.isNoneBlank(phone)) {
			    if(!phone.equals(user.getPhone())) {
			    	return new Paged<>();
			    }
			}
			map.put("phone", user.getPhone());
		}
		
		Paged<BlacklistDto> blacklistPaged = blacklistService.getBlacklistList(map);
		if(null == blacklistPaged) {
			return  new Paged<>();
		}else if(blacklistPaged.isEmpty()) {
			return  new Paged<>(null,blacklistPaged.getTotal(),blacklistPaged.getPageSize());
		}
		
		List<BlacklistVo> blacklistVoList = Convert.toBlacklistVo(blacklistPaged.getList());
		for (BlacklistVo vo : blacklistVoList) {
			UserDto user = userService.getUserByPhone(vo.getPhone());
			if(null != user) {
				vo.setUserName(user.getName());
				vo.setUserId(user.getId());
			}
		}
		
		Integer pageSize=blacklistPaged.getPageSize();
		Long  total =blacklistPaged.getTotal();
		Paged<BlacklistVo> paged=new Paged<>(blacklistVoList,total,pageSize);
		
		return paged;
	}
	
	/**
	 * 创建黑名单
	 * 
	 * @param createVo
	 * @return
	 */
	@Override
	public Boolean createBlacklist(BlacklistCreateVo createVo) {
		Integer userId = createVo.getUserId();
		String phone = createVo.getPhone();
		if(null == userId && StringUtils.isBlank(phone)) {
			throw new BizException(101,"参数不能为空");
		}
		
		if(null == userId && StringUtils.isNoneBlank(phone)) {
			UserDto user= userService.getUserByPhone(phone);
			if(null == user) {
				throw new BizException(101,"不存在该用户");
			}
			userId=user.getId();
		}
		
		UserBizDto userBizDto = userBizService.getUser(userId);
		if(null == userBizDto || null == userBizDto.getId()) {
			throw new BizException(101,"不存在该用户");
		}
		BlacklistCreateDto createDto=new BlacklistCreateDto();
		BeanUtils.copyProperties(createVo, createDto);
		createDto.setOperationTime(DateUtil.convertDateToLDT(new Date()));
		createDto.setType(BlacklistTypeEnum.MANUAL.getValue());
		createDto.setPhone(userBizDto.getPhone());
		createDto.setIdNumber(userBizDto.getIdNumber());
		createDto.setName(userBizDto.getRealName());
		Boolean flag = blacklistService.createBlacklist(createDto);
		if(!flag) {
			return flag;
		}
		
		flag = userService.modifyIsBlack(userBizDto.getId(), true);
		
		return flag;
	}
	
	/**
	 * 移除黑名单
	 * 
	 * @param modifyVo
	 * @return
	 */
	@Override
	public Boolean deleteBlacklist(BlacklistModifyVo modifyVo) {
		
		BlacklistDto blacklistDto = blacklistService.getBlacklist(modifyVo.getBlacklistId());
		if(null == blacklistDto) {
			throw new BizException(101,"不存在该黑名单信息");
		}
		
		UserDto userDto = userService.getUserByPhone(blacklistDto.getPhone());
		if(null == userDto || null == userDto.getId()) {
			throw new BizException(101,"不存在该用户");
		}
		
		//修改用户是否拉入黑名单状态
		Boolean flag = userService.modifyIsBlack(userDto.getId(), false);
		if(!flag) {
			return flag;
		}
		
		flag = blacklistService.deleteBlacklist(blacklistDto.getBlacklistId(), modifyVo.getOperateId(), modifyVo.getOperateName());
		
		return flag;
	}
	
	
	/**
	 * 移除黑名单
	 * 
	 * @param modifyVo
	 * @return
	 */
	@Override
	public Boolean deleteUserBlacklist(BlacklistModifyVo modifyVo) {
		UserDto user = userService.getUser(modifyVo.getUserId());
		if(null == user) {
			throw new BizException(101,"不存在该用户");
		}
		BlacklistDto blacklistDto = blacklistService.getBlacklist(user.getPhone());
		if(null == blacklistDto) {
			throw new BizException(101,"该用户不是在黑名单");
		}
		//修改用户是否拉入黑名单状态
		Boolean flag = userService.modifyIsBlack(user.getId(), false);
		if(!flag) {
		  return flag;
		}
		flag = blacklistService.deleteBlacklist(blacklistDto.getBlacklistId(), modifyVo.getOperateId(), modifyVo.getOperateName());
		return flag;
	}
	
	/**
	 * 根据userId查询最近一条征信信息
	 * @param userId
	 * @return
	 */
	@Override
	public CreditRecognitionVo getCreditRecognition(Integer userId) {
		CreditRecognitionDto verifyResult = creditRecognitionService.getVerifyResult(userId);
		CreditRecognitionVo vo = Convert.toCreditRecognitionVo(verifyResult);
		return vo;
	}
}
